<template>
	<view class="container">
		<z-paging ref="paging" v-model="roomList" @query="queryList" :auto="false">
			<view slot="top">
				<view class="search-header">
					<view class="search-bar-box">
						<view class="search-bar">
							<u-icon name="search" size="20" color="#999999"></u-icon>
							<input type="text" placeholder="请输入关键字搜索" v-model="keyword" @input="$u.debounce(refreshList, 500)" placeholder-class="placeholder" />
						</view>
						<!-- <u-icon name="plus-circle" size="20" color="#FE4F04"></u-icon> -->
					</view>
					<view class="filter-section">
						<!-- 	<view class="sort-options">
							<view class="sort-item" @click="handleSort('roomNumber')">
								<text>房号</text>
								<image src="/pages-landlord/static/room/sort-no.png"
									v-if="sortConfig.field!='roomNumber'">
								</image>
								<image src="/pages-landlord/static/room/sort-up.png"
									v-else-if="sortConfig.field=='roomNumber' && sortConfig.order=='asc'"></image>
								<image src="/pages-landlord/static/room/sort-down.png"
									v-else-if="sortConfig.field=='roomNumber' && sortConfig.order=='desc'"></image>
							</view>
							<view class="sort-item" @click="handleSort('price')">
								<text>价格</text>
								<image src="/pages-landlord/static/room/sort-no.png" v-if="sortConfig.field!='price'">
								</image>
								<image src="/pages-landlord/static/room/sort-up.png"
									v-else-if="sortConfig.field=='price' && sortConfig.order=='asc'"></image>
								<image src="/pages-landlord/static/room/sort-down.png"
									v-else-if="sortConfig.field=='price' && sortConfig.order=='desc'"></image>
							</view>
							<view class="sort-item" @click="handleSort('date')">
								<text>下次收租</text>
								<image src="/pages-landlord/static/room/sort-no.png" v-if="sortConfig.field!='date'">
								</image>
								<image src="/pages-landlord/static/room/sort-up.png"
									v-else-if="sortConfig.field=='date' && sortConfig.order=='asc'"></image>
								<image src="/pages-landlord/static/room/sort-down.png"
									v-else-if="sortConfig.field=='date' && sortConfig.order=='desc'"></image>
							</view>
						</view> -->
						<view class="filter-tabs">
							<u-tabs :list="tabsList" :current="tabsIndex" @click="tabsChange" lineWidth="20"
								lineColor="#FE4F04" :scrollable="false" :itemStyle="{padding:'22rpx 0'}">
							</u-tabs>
						</view>
					</view>
				</view>
			</view>
			<view class="room-list">
				<view v-for="(item, index) in roomList" :key="index" class="room-item">
					<view class="checkbox-group" v-if="showSelect">
						<u-checkbox-group shape="circle" v-model="selectRoomList">
							<u-checkbox :name="item.id"></u-checkbox>
						</u-checkbox-group>
					</view>
					<view class="room-info">
						<view class="room-number">{{item.building.address}}{{ item.name }}</view>
						<view class="room-details">
							<text> {{ item.floor_no }}楼 </text>
							<text> {{ item.area }}m² </text>
							<text> {{ item.house_type }} </text>
						</view>
						<!-- <view class="room-details">{{ item.details }}</view> -->
					</view>
					<view class="room-status">
						<view class="price">
							<!-- <text v-if="item.isDiscounted" class="original-price">¥{{ item.originalPrice }}/月</text> -->
							<text class="current-price">¥{{ item.price }}/月</text>
						</view>
						<!-- <view class="date">{{ item.date }}</view> -->
						<!-- <view v-if="item.isEmpty" class="empty-days">空置 {{ item.emptyDays }} 天</view> -->
						<view class="status-tag" v-if="item.type_text">
							<u-icon name="man-add" size="12" color="#67C23A"></u-icon>
							<text>{{item.type_text}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- <view slot="bottom">
				<view class="safe-bottom action-bar" v-if="showSelect">
					<view class="radio-group">
						<image src="/pages-landlord/static/room/icon-xuan.png" v-if="isAllSelect"
							@click="changeAllSelect"></image>
						<image src="/pages-landlord/static/room/icon-xuan1.png" v-if="!isAllSelect"
							@click="changeAllSelect"></image>
					</view>
					<view class="action-item" @click="handleAdd">添加</view>
					<view class="action-item" @click="handleEdit">修改</view>
					<view class="action-item" @click="handleGenerate">生成</view>
					<view class="action-item" @click="handleDelete">删除</view>
				</view>
				<view class="safe-bottom action-bar" v-if="!showSelect">
					<view class="action-item" @click="handleBatch">批量</view>
					<view class="action-item" @click="handleAll">全部大楼</view>
					<view class="action-item" @click="handleFilter">筛选</view>
				</view>
			</view> -->
		</z-paging>
		<u-popup :show="showPopup" mode="bottom" :closeOnClickOverlay="false" round="40rpx">
			<view class="filter-modal">
				<view class="header">
					<text @click="handleCancel">取消</text>
					<text style="font-weight: bold">筛选</text>
					<text class="confirm" @click="handleConfirm">确认</text>
				</view>
				<view class="fill-box"></view>
				<view class="section-box">
					<view class="title">收租</view>
					<view class="tags-box">
						<view :class="queryForm.rentPeriod==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in rentPeriods" :key="index" @click="handleRentPeriod(item)">
							{{item.text}}
						</view>
					</view>
				</view>
				<view class="section-box">
					<view class="title">指定</view>
					<view class="date-range">
						<view class="date-item" @click="showStartDate= true">
							<view v-if="queryForm.startDate">{{queryForm.startDate}}</view>
							<view class="placeholder" v-if="!queryForm.startDate">请选择</view>
						</view>
						<text>至</text>
						<view class="date-item" @click="showEndDate= true">
							<view v-if="queryForm.endDate">{{queryForm.endDate}}</view>
							<view class="placeholder" v-if="!queryForm.endDate">请选择</view>
						</view>
					</view>
				</view>
				<view class="section-box">
					<view class="title">提前</view>
					<view class="input-box">
						<input type="number" placeholder="0:表示关闭提醒" v-model="queryForm.remindDay" />
						<text>提醒</text>
					</view>
				</view>
				<view class="section-box">
					<view class="title">状态</view>
					<view class="tags-box">
						<view :class="queryForm.status==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in statusOptions" :key="index" @click="handleStatus(item)">
							{{item.text}}
						</view>
					</view>
				</view>
				<view class="section-box">
					<view class="title">类型</view>
					<view class="tags-box">
						<view :class="queryForm.propertyType==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in propertyTypes" :key="index" @click="handlePropertyType(item)">
							{{item.text}}
						</view>
					</view>
				</view>
				<view class="section-box">
					<view class="title">户型</view>
					<view class="tags-box">
						<view :class="queryForm.roomType==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in roomTypes" :key="index" @click="handleRoomType(item)">
							{{item.text}}
						</view>
					</view>
				</view>
			</view>
			<u-datetime-picker :show="showStartDate" v-model="startDate" mode="date" @confirm="onStartDateConfirm"
				@cancel="onStartDateCanceel" />
			<u-datetime-picker :show="showEndDate" v-model="endDate" mode="date" @confirm="onEndDateConfirm"
				@cancel="onEndDateCanceel" />
		</u-popup>
	</view>
</template>
<script>
	import {
		buildingStatList,
	} from "@/api/landlord"
	export default {
		data() {
			return {
				sortConfig: {
					field: '',
					order: '' // 'asc' | 'desc' | ''
				},
				tabsList: [],
				tabsIndex: 0,
				showSelect: false,
				selectRoomList: [],
				isAllSelect: false,
				roomList: [],
				showPopup: false,
				showStartDate: false,
				showEndDate: false,
				queryForm: {
					rentPeriod: '',
					status: '',
					propertyType: '',
					roomType: '',
					startDate: '',
					endDate: '',
					remindDay: '' // 提前提醒天数
				},
				keyword:'',
				startDate: '',
				endDate: '',
				rentPeriods: [{
					text: '今天',
					value: 1
				}, {
					text: '明天',
					value: 2
				}, {
					text: '本周',
					value: 3
				}, {
					text: '本月',
					value: 4
				}],
				statusOptions: [{
					text: '全部',
					value: 1
				}, {
					text: '已租',
					value: 2
				}, {
					text: '空房',
					value: 3
				}, {
					text: '押满',
					value: 4
				}],
				propertyTypes: [{
					text: '居住',
					value: 1
				}, {
					text: '商铺',
					value: 2
				}, {
					text: '厂房',
					value: 3
				}, {
					text: '办公',
					value: 4
				}, {
					text: '仓库',
					value: 5
				}, {
					text: '建材',
					value: 6
				}, {
					text: '物业',
					value: 7
				}, {
					text: '车位',
					value: 8
				}],
				roomTypes: [{
					text: '2室1厅',
					value: 1
				}, {
					text: '单间',
					value: 2
				}, {
					text: '1室0厅1卫',
					value: 3
				}, {
					text: '3室1厅',
					value: 4
				}, {
					text: '4室1厅',
					value: 5
				}]
			};
		},
		onLoad(options) {
			if (options && options.type) {
				this.tabsIndex = Number(options.type)
			}
			this.refreshList();
		},
		methods: {
			handleRentPeriod(item) {
				this.queryForm.rentPeriod = item.value;
			},
			handleStatus(item) {
				this.queryForm.status = item.value;
			},
			handlePropertyType(item) {
				this.queryForm.propertyType = item.value;
			},
			handleRoomType(item) {
				this.queryForm.roomType = item.value;
			},
			onStartDateConfirm(event) {
				this.startDate = event.value
				//格式化时间戳YYYY-MM-DD
				this.queryForm.startDate = uni.$u.timeFormat(event.value, 'yyyy-mm-dd')
				this.showStartDate = false
			},
			onStartDateCanceel() {
				this.startDate = ''
				this.queryForm.startDate = ''
				this.showStartDate = false
			},
			onEndDateConfirm(event) {
				this.endDate = event.value
				//格式化时间戳YYYY-MM-DD
				this.queryForm.endDate = uni.$u.timeFormat(event.value, 'yyyy-mm-dd')
				this.showEndDate = false
			},
			onEndDateCanceel() {
				this.endDate = ''
				this.queryForm.endDate = ''
				this.showEndDate = false
			},
			handleCancel() {
				this.startDate = ''
				this.endDate = ''
				this.showPopup = false
			},
			handleConfirm() {
				this.showPopup = false
			},
			getTabsListCount() {
				this.tabsList = [
					// 	{
					// 	name: `全部`,
					// 	status: '',
					// }, 
					{
						name: `已出租`,
						status: '1',
					}, {
						name: `发布中`,
						status: '2',
					}, {
						name: `未出租`,
						status: '3',
					}
				];
				// 获取各个状态的数量
				// this.$Api.user.getAttendanceCount({
				//   year: this.year,
				//   month: this.month
				// }).then(res => {
				//   this.tabsList = [{
				//     name: `未开始(${res.data[0] || 0})`,
				//     status: '1',
				//   }, {
				//     name: `进行中(${res.data[1] || 0})`,
				//     status: '2',
				//   }, {
				//     name: `已结束(${res.data[2] || 0})`,
				//     status: '3',
				//   }];
				// });
			},
			tabsChange(item) {
				this.courseList = []
				this.tabsIndex = item.index
				this.refreshList()
			},
			refreshList() {
				if (this.$refs.paging) {
					this.$refs.paging.refresh();
					this.getTabsListCount();
				}
			},
			queryList(pageNo, pageSize) {
				buildingStatList({
					page: pageNo,
					page_size: pageSize,
					kw:this.keyword,
					flag: this.tabsList[this.tabsIndex].status
				}).then(res => {
					this.$refs.paging.complete(res.data.data.data);
				}).catch(res => {
					this.$refs.paging.complete(false);
				})
			},
			handleSort(field) {
				if (this.sortConfig.field === field) {
					this.sortConfig.order = this.sortConfig.order === 'asc' ? 'desc' : 'asc';
				} else {
					this.sortConfig.field = field;
					this.sortConfig.order = 'asc';
				}
			},
			switchTab(tab) {
				this.activeTab = tab;
			},
			handleBatch() {
				this.showSelect = true;
				this.selectRoomList = [];
			},
			handleAll() {
				console.log('全部大楼');
			},
			handleFilter() {
				console.log('筛选');
				this.showPopup = true
			},
			changeAllSelect() {
				this.isAllSelect = !this.isAllSelect
			},
			handleAdd() {
				console.log('添加');
				this.showSelect = false;
			},
			handleEdit() {
				console.log('修改');
			},
			handleGenerate() {
				console.log('生成');
			},
			handleDelete() {
				console.log('删除');
			}
		}
	};
</script>
<style lang="scss" scoped>
	.filter-modal {
		background: #fff;
		height: 70vh;
		border-radius: 40rpx 40rpx 0 0;
		overflow-y: scroll;

		.header {
			position: fixed;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 40rpx;
			box-sizing: border-box;
			height: 120rpx;
			border-radius: 40rpx 40rpx 0 0;
			border-bottom: 1px solid #eee;
			background: #ffffff;
			z-index: 100;

			.confirm {
				color: #FE4F04;
			}
		}

		.fill-box {
			height: 120rpx;
		}

		.section-box {
			padding: 0 20rpx;
			margin: 20rpx 0 40rpx;

			.title {
				font-size: 28rpx;
				color: #222222;
				font-weight: 600;
				margin-bottom: 20rpx;
			}

		}

		.tags-box {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;

			.tag-item {
				font-size: 28rpx;
				height: 68rpx;
				line-height: 68rpx;
				padding: 0rpx 50rpx;
				border-radius: 40rpx;
				background: #F5F6FA;
				border: 1rpx solid #F5F6FA;
			}

			.tag-item.active {
				color: #FF7D41;
				background: #FFF6ED;
				border: 1rpx solid #FE4F04;
			}
		}

		.date-range,
		.input-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: 20rpx;

			input {
				flex: 1;
				height: 68rpx;
				background: #F5F6FA;
				border-radius: 40rpx;
				line-height: 68rpx;
				padding: 0 20rpx;
				margin-right: 20rpx;
			}

			.date-item {
				width: 302rpx;
				height: 68rpx;
				background: #F5F6FA;
				border-radius: 40rpx;
				line-height: 68rpx;
				text-align: center;

				.placeholder {
					font-size: 28rpx;
					color: #999999;
				}
			}

			text {
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
				text-align: center;
			}
		}
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
		background-color: #F5F6F7;
	}

	.search-header {
		flex-shrink: 0;
		background-color: #FFFFFF;
		padding: 20rpx 30rpx;

		.search-bar-box {
			display: flex;
			align-items: center;

			.search-bar {
				flex: 1;
				border-radius: 36rpx;
				display: flex;
				align-items: center;
				background-color: #F5F6F7;
				padding: 16rpx 24rpx;
				margin-right: 20rpx;
			}

			.search-bar input {
				flex: 1;
				margin: 0 20rpx;
				font-size: 14px;
			}

			.placeholder {
				color: #999999;
			}
		}

	}



	.filter-section {
		margin-top: 24rpx;

		.sort-options {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 24rpx;

			.sort-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 18rpx 24rpx;
				width: 218rpx;
				height: 72rpx;
				box-sizing: border-box;
				background: #F5F6FA;
				border-radius: 12rpx;

				text {
					font-weight: 500;
					font-size: 26rpx;
					color: #202020;
				}

				image {
					width: 12rpx;
					height: 24rpx;
				}
			}
		}
	}

	.room-list {
		flex: 1;
		overflow: auto;
		padding: 20rpx;

		.room-item {
			display: flex;
			align-items: center;
			background-color: #FFFFFF;
			padding: 30rpx;
			border-radius: 12rpx;
			margin-bottom: 20rpx;

			.checkbox-group {
				margin-right: 32rpx;
			}

			.room-info {
				flex: 1;
				margin-right: 20rpx;
			}

			.room-number {
				font-size: 30rpx;
				color: #333333;
				font-weight: 500;
				margin-bottom: 12rpx;
			}

			.room-address {
				font-size: 26rpx;
				color: #444444;
				margin-bottom: 8rpx;
			}

			.room-details {
				font-size: 24rpx;
				color: #999999;
				display: flex;
				gap: 10rpx;
			}

			.room-status {
				text-align: right;
			}

			.price {
				margin-bottom: 8rpx;
			}

			.original-price {
				font-size: 24rpx;
				color: #D1D2D4;
				text-decoration: line-through;
				margin-right: 8rpx;
			}

			.current-price {
				font-size: 28rpx;
				color: #FE4F04;
				font-weight: 500;
			}

			.date {
				font-size: 26rpx;
				color: #666666;
				margin-bottom: 8rpx;
			}

			.empty-days {
				font-size: 12px;
				color: #FF6B35;
			}

			.status-tag {
				display: inline-flex;
				align-items: center;
				background-color: #F0F9EB;
				padding: 4rpx 12rpx;
				border-radius: 4rpx;
			}

			.status-tag text {
				font-size: 12px;
				color: #67C23A;
				margin-left: 4rpx;
			}
		}
	}





	.action-bar {
		width: 100%;
		height: 102rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 20rpx 32rpx;
		background-color: #FFFFFF;

		.radio-group {
			display: flex;
			align-items: center;
			width: 70rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}
		}

		.action-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			flex: 1;
			text-align: center;
		}
	}
</style>